import Vue from 'vue'

// 获取验证码倒计时
Vue.directive('timer', {
    bind : function (el, binding) {
        let t
        el.addEventListener("click", function () {
            if (el.dataset.validate==='true') return
            let s = binding.arg
                ? binding.arg
                : 60
            const oldText = el.innerText
            el.disabled = true
            el.innerText = oldText + "（" + s + "S）"
            t = setInterval(function (params) {
                if (s === 1) {
                    if (binding.value && "function" === typeof(binding.value)) {
                        binding.value()
                    }
                    el.innerText = oldText
                    el.disabled = false
                    clearInterval(t)
                    return
                }
                s-- 
                el.innerText = oldText + "（" + s + "S）"
            }, 1000)
        })
    }
})
